package com.taptech.doufu.presenter.home;

import android.app.Activity;
import android.content.Context;

import com.google.gson.Gson;
import com.taptech.doufu.constant.HomePageType;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.bean.base.BaseResponseBean;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.util.GsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Wyatt on 2017/10/13/013.
 */

public class HomeBannerPresenter {

    public interface OnHomeBannerView{
        void onBannerNext(List<DFHomeBannerBean> list, boolean isCache);
    }

    private OnHomeBannerView onHomeBannerView;
    private Context mContext;
    private String pageType;

    public HomeBannerPresenter(Context context,String pageType,OnHomeBannerView onHomeBannerView){
        this.onHomeBannerView = onHomeBannerView;
        mContext = context;
        this.pageType = pageType;
    }


    private String getUrl() {
        if (HomePageType.RAINBOW_FINISHED.equals(pageType)){
            return "/banner/get?place=finished.block&sexType=1";
        }else if (HomePageType.COPYRIGHT.equals(pageType)){
            return "/banner/get?place=copyright.block";
        }else if(HomePageType.RAINBOW.equals(pageType)){
            return "/index.php/banner/get?place=home.novel&sexType=1";
        }else{
            return "/index.php/banner/get?place=home.novel&sexType=3";
        }
    }

    private String getCachePath() {
        if (HomePageType.RAINBOW_FINISHED.equals(pageType)){
            return CommmonImp.Home_novel_banner_finished;
        }else if (HomePageType.COPYRIGHT.equals(pageType)){
            return CommmonImp.Home_novel_banner_copyright;
        }else if (HomePageType.RAINBOW.equals(pageType)){
            return CommmonImp.Home_novel_banner;
        }else{
            return CommmonImp.Home_novel_UN_banner;
        }
    }


    public void loadBannerCache() {
        Observable.create((Observable.OnSubscribe<List<DFHomeBannerBean>>) subscriber -> {
            List<DFHomeBannerBean> list = null;
            try {
                JSONObject json = CacheUtil.readJsonDataFromCache(new File(getCachePath()));
                list = GsonUtil.jsonToList(json.getJSONArray("data").toString(), DFHomeBannerBean[].class);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            subscriber.onNext(list);
        })
                .compose(RxJavaHelper.<List<DFHomeBannerBean>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<List<DFHomeBannerBean>>(){
                    @Override
                    public void onNext(List<DFHomeBannerBean> list) {
                        super.onNext(list);
                        if (onHomeBannerView != null && list!=null) {
                            onHomeBannerView.onBannerNext(list,true);
                        }
                    }
                });
    }

    /**
     * 获取轮播图数据彩虹、言情
     */
    public void getHomeNovelBanner(final String sexType,boolean isUseCache) {
        ApiClient.getInstance().setUseCache(isUseCache).setSexType(sexType).getService().getNovelBanner(getUrl())
                .map(new Func1<BaseResponseBean<List<DFHomeBannerBean>>, BaseResponseBean<List<DFHomeBannerBean>>>() {
                    @Override
                    public BaseResponseBean<List<DFHomeBannerBean>> call(BaseResponseBean<List<DFHomeBannerBean>> listBaseResponseBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(listBaseResponseBean),new File(getCachePath()));
                        return listBaseResponseBean;
                    }
                })
                .compose(RxJavaHelper.<BaseResponseBean<List<DFHomeBannerBean>>>observeOnMainThread((Activity) mContext))
                .subscribe(new BaseSubscriber<BaseResponseBean<List<DFHomeBannerBean>>>() {
                    @Override
                    public void onNext(BaseResponseBean<List<DFHomeBannerBean>> novelBanner) {
                        super.onNext(novelBanner);
                        if (onHomeBannerView != null && novelBanner!=null) {
                            onHomeBannerView.onBannerNext(novelBanner.getData(),false);
                        }
                    }
                });
    }

}
